package org.hdl.anima.session;

import java.net.InetSocketAddress;

import org.hdl.anima.AppConf;
import org.hdl.anima.transport.Channel;
import org.hdl.anima.transport.ChannelHandler;
import org.hdl.anima.transport.RemoteException;
import org.hdl.anima.utils.StringUtils;

import static com.google.common.base.Preconditions.checkArgument;
/**
 * 
 * @author qiuhd
 * @since  2014-8-1
 * @version V1.0.0
 */
public class ClientSession implements ISession {
	
	private Channel channel;
	
	private int  identity;
	/**
	 * 客户端与服务器重连token
	 */
	private String reconnectToken;
	/**
	 * 会话所在服务器Id
	 */
	private String serverId;
	/**
	 * session close listener
	 */
	private SessionCloseListener listener ;
	/**
	 * Client type
	 */
	private String clientType;
	
	public ClientSession(String serverId,int identity,Channel channel) {
		checkArgument(StringUtils.isNotEmpty(serverId), "serverId name can not be null");
//		checkArgument(StringUtils.isNotEmpty(identity), "identity can not be null");
		checkArgument(channel != null,"channel can not be null");
		this.channel = channel;
		this.serverId = serverId ;
		this.identity = identity;
	}
	
	@Override
	public String getServerId() {
		return this.serverId;
	}

	@Override
	public int getIdentity() {
		return this.identity;
	}
	
	public String getReconnectToken() {
		return this.reconnectToken ;
	}

	@Override
	public boolean isConnected() {
		return channel.isConnected();
	}

	@Override
	public InetSocketAddress getRemoteAddress() {
		return channel.getRemoteAddress();
	}

	@Override
	public Object getAttribute(String key) {
		return channel.getAttribute(key);
	}

	@Override
	public void setAttribute(String key, Object object) {
		channel.setAttribute(key, object);
	}

	@Override
	public boolean contains(String key) {
		return channel.contains(key);
	}

	@Override
	public void removeAttribute(String key) {
		this.removeAttribute(key);
	}

	@Override
	public AppConf getConf() {
		return channel.getConf();
	}

	@Override
	public InetSocketAddress getLocalAddress() {
		return channel.getLocalAddress();
	}

	@Override
	public ChannelHandler getChannelHandler() {
		return channel.getChannelHandler();
	}

	@Override
	public void send(Object message) throws RemoteException {
		this.channel.send(message);
	}

	@Override
	public void close() {
		this.channel.close();
		if (listener != null) {
			listener.onSessionClosed(this);
		}
	}

	@Override
	public void close(int timeout) {
		this.channel.close(timeout);
	}

	@Override
	public boolean isClosed() {
		return channel.isClosed();
	}

	@Override
	public void setlistener(SessionCloseListener listener) {
		if(this.listener != null) {
			throw new IllegalArgumentException("Failed to set up listner,cause :listener already set up");
		}
		
		this.listener = listener ;
	}

	public String getClientType() {
		return clientType;
	}

	public void setClientType(String clientType) {
		this.clientType = clientType;
	}
}

